package com.zlm.app.emp.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.util.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文件上传控制器
 * @author xhj
 */
@RestController
@RequestMapping(value = "/fileUpload")
public class FileUploadController extends BaseController {

    /**
     * 单个文件上传
     * @param request
     * @param fileType 1 头像
     * @return
     */
    @PostMapping("/upload")
    public JsonResultDto upload(HttpServletRequest request, String fileType) {
        try {
            List<String> files = FileUtils.fileUpload(request, fileType);
            if (files.size() > 0) {
                return success(files.get(0));
            }

            return fail();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 多个文件上传
     * @param request
     * @param fileType
     * @return
     */
    @PostMapping("/uploads")
    public JsonResultDto uploads(HttpServletRequest request, String fileType) {
        try {
            List<String> files = FileUtils.fileUpload(request, fileType);
            if (files.size() > 0) {
                return success(files);
            }

            return fail();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }
}
